<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片试图区域 -->
  <el-card>
      <!-- 搜索区域 -->
        <!-- :gutter 按钮和搜索框之间的距离 -->
        <el-row>
            <el-col :span="9">
                <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrderList">
                    <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
                </el-input>
            </el-col>
        </el-row>  

        <!-- 订单列表数据 -->
        <el-table :data="orderList" border stripe>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="订单数据" prop="order_number"></el-table-column>
            <el-table-column label="订单价格" prop="order_price"></el-table-column>
            <el-table-column label="是否支付" prop="pay_status">
                <template v-slot="scope">
                    <el-tag type="danger" v-if="scope.row.pay_status==='0'">未支付</el-tag>
                    <el-tag type="success" v-else>未支付</el-tag>
                </template>  
            </el-table-column>
            <el-table-column label="是否发货" prop="is_send"></el-table-column>
            <el-table-column label="下单时间" prop="create_time">
                <template v-slot="scope">
                    {{scope.row.create_time | dataFormat}}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <!-- eslint-disable-next-line -->
                <template v-slot="scope">
                    <el-button size="mini" type="primary" icon="el-icon-edit" @click="locationVisible = true"></el-button>
                    <el-button size="mini" type="success" icon="el-icon-location" @click="showProgressBox"></el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页功能 -->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[5, 7, 10, 15]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total" background>
        </el-pagination> 
    </el-card>


    <!-- 修改地址谈话框 -->
    <el-dialog
        title="修改地址"
        :visible.sync="locationVisible"
        width="50%" @close="editAddressClose">
        <el-form :model="addressForm" :rules="addressRules" ref="addressRef" label-width="100px">
            <el-form-item label="省市区/县" prop="address1">
                <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
            </el-form-item>
            <el-form-item label="详细地址" prop="address2">
                <el-input v-model="addressForm.address2"></el-input>
            </el-form-item>
        </el-form>    
        <span slot="footer" class="dialog-footer">
                <el-button @click="locationVisible = false">取 消</el-button>
                <el-button type="primary" @click="locationVisible = false">确 定</el-button>
            </span>
    </el-dialog>


    <!-- 展示物流进度谈话框 -->
    <el-dialog
        title="物流进度"
        :visible.sync="showProgressVisible"
        width="50%" @close="showProgressClose">
        <!-- timeline时间线 -->
        <el-timeline>
            <el-timeline-item
            v-for="(activity, index) in ProgressInfo" :key="index"
            type="success" icon="el-icon-check" :timestamp="activity.time">
            {{activity.context}}</el-timeline-item>
        </el-timeline>
    </el-dialog>

  </div>
</template>

<script>
import cityData from '../../assets/js/citydata.js'
export default {
    name: 'Order',
    data() {
        return {
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 7
            },
            // 订单数据条数
            total:0,
            // 获取到的订单数据
            orderList: [],
            // 控制修改地址谈话框的显示与隐藏
            locationVisible: false,
            // 表单数据对象
            addressForm:{
                address1: [],
                address2: ''
            },
            // 验证规则
            addressRules:{
                address1:[
                    { required: true, message: '请选择省市区/县', trigger: 'blur' },
                ],
                address2:[
                    { required: true, message: '请填写详细地址', trigger: 'blur' },
                ],
            },
            cityData,
            // 控制物流进度的谈话框 显示与隐藏
            showProgressVisible: false,
            // 物流进度信息
            ProgressInfo:[]
        }
    },
    created() {
        this.getOrderList()    
    },
    methods: {
        async getOrderList(){
            const {data: res} = await this.$http.get('orders',{params: this.queryInfo})
            // console.log(res);
            if(res.meta.status !== 200) return this.$message.error('获取订单列表数据失败！')
            this.$message.success('获取订单列表数据成功！')
            this.orderList = res.data.goods
            this.total = res.data.total
        },
        // 监听每页显示的条数
        handleSizeChange(newsize){
            this.queryInfo.pagesize = newsize
            this.getOrderList()
        },
        // 监听页码的变化
        handleCurrentChange(newpage){
            this.queryInfo.pagenum = newpage
            this.getOrderList()
        },
        // 监听修改地址对话框的关闭事件
        editAddressClose(){
            this.$refs.addressRef.resetFields()
        },
        // 展示物流进度
        async showProgressBox(id){
            const {data: res} =  await this.$http.get(`/kuaidi/1106975712662`)
            console.log(res);
            if(res.meta.status !== 200) return this.$message.error('获取物流信息失败！')
            this.ProgressInfo = res.data
            this.showProgressVisible = true
        },
        // 监听物流进度对话框的关闭事件
        showProgressClose(){
            // this.$refs
        }
    },
}
</script>

<style scoped>
    .el-cascader{
        width: 100%;
    }
</style>